//
//  STLeftViewController.swift
//  STMusic
//
//  Created by stoneobs on 16/10/18.
//  Copyright © 2016年 stoneobs. All rights reserved.
//  这是个人中心

import UIKit
class YYLeftViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{

    var tableView:UITableView?;
    let datasouceArray = [["我的歌单","缓存列表"],["关于我们","意见反馈"],["设置"]];
    
    var imageView:UIImageView?
    var visoulView:UIVisualEffectView?
    
    var iconButton:STButton?
    
    var nickNameLable:STLabel?
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.configSubView();
        
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:FlatMintDark()];
        
        
        let quere = BmobUser.query();
        
        quere?.findObjectsInBackground({ (array, error) in
            
        });
    }

    func configSubView()  {
        self.view.backgroundColor = BACKROUND_COLOR;
        tableView = UITableView.init(frame: SCREEN_FRAME, style: .grouped);
        tableView?.delegate = self;
        tableView?.dataSource = self;
        self.tableView?.backgroundColor = .clear;
        view.addSubview(tableView!);
        
        self.imageView = UIImageView.init(frame:CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: 250));
        self.imageView?.sd_setImage(with: URL.init(string: YYUserLoginedDefultManger.shared().currentUser().iconUrl!), placeholderImage: ThemePeopleImage, options: .retryFailed);
        self.view.insertSubview(self.imageView!, at: 0);
        let visEffect = UIBlurEffect.init(style: .light);
        visoulView = UIVisualEffectView.init(frame: (imageView?.bounds)!)
        visoulView?.effect = visEffect;
        visoulView?.alpha = 0.75;
        imageView?.addSubview(visoulView!);
        
        
        let headerView = UIView.init(frame: .init(x: 0, y: 0, width: SCREEN_WIDTH, height: 200))
        headerView.backgroundColor = .clear;
        self.iconButton = STButton.init(frame: .init(x: 0, y: 110, width: 80, height: 80),
                                       title: nil,
                                       titleColor: nil,
                                       titleFont: 0,
                                       cornerRadius: 40,
                                       backgroundColor: nil,
                                       backgroundImage: ThemePeopleImage,
                                       image: nil);
        iconButton?.centerX = SCREEN_WIDTH / 2;
        iconButton?.showRightShadow();
        iconButton?.isUserInteractionEnabled = false;
        iconButton?.setBrderWidth(1.5, borderColor: YYThemeStyleManger.defultManger.currentColor);
        iconButton?.centerY = headerView.height / 2;
        headerView.addSubview(iconButton!);
        
        self.nickNameLable = STLabel.init(frame: .init(x: 35, y: 0, width: 300, height: 13),
                                          text: YYUserLoginedDefultManger.shared().currentUser().nickName,
                                          textColor: ThirdTextColor,
                                          font: 14,
                                          isSizetoFit: false,
                                          textAlignment: .center);
        self.nickNameLable?.top = (iconButton?.bottom)! + 10;
        self.nickNameLable?.centerX = SCREEN_WIDTH / 2;
        headerView.addSubview(self.nickNameLable!);

        tableView?.tableHeaderView = headerView;
        tableView?.showsVerticalScrollIndicator = false;
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated);
        self.hideNavagetionbar();
        //STLayoutViewController.defaultLayout().rootViewController.view.left = SCREEN_WIDTH - 40;
    }
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated);
        self.showNavagationbar();
    }

    // MARK: UITableView DataSouce
    func numberOfSections(in tableView: UITableView) -> Int {
        return datasouceArray.count;
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datasouceArray[section].count;
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44;
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 10;
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCell(withIdentifier: "cell");
        if cell == nil {
            cell = UITableViewCell.init(style: .value1, reuseIdentifier: "cell");
        }
        cell?.textLabel?.text = datasouceArray[indexPath.section][indexPath.row];
        cell?.textLabel?.font = Font15;
        cell?.textLabel?.textColor = FirstTextColor;
        let imagesNames = [["img_myMusicList","img_cacheList"],["img_aboutUs","img_ suggest"],["img_setting"]];
        cell?.imageView?.image = UIImage.init(named: imagesNames[indexPath.section][indexPath.row]);
        cell?.accessoryType = .disclosureIndicator;
        return cell!;
    }
    
    // MARK: UITableView Delegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false);
    
        if indexPath.section == 1 && indexPath.row == 0 {
            
             self.navigationController?.pushViewController(YYAboutUsViewController(), animated: true);
        }
        if indexPath.section == 1 && indexPath.row == 1 {
            
            self.navigationController?.pushViewController(YYSuggestViewController(), animated: true);
        }
        if indexPath.section == 2  {
            self.navigationController?.pushViewController(YYSettingViewController(), animated: true);
        }
        
    }
    // MARK: UITableView Delegate
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        
        let  height = scrollView.contentOffset.y;
        if height > 0 {
            self.imageView?.top = 0 -  1 * height;
        }else{
        
            self.imageView?.height = 250  +  abs(height);
            self.imageView?.width = (self.imageView?.height)! *  SCREEN_WIDTH / 250;
            self.imageView?.top = 0;
            self.imageView?.centerX = SCREEN_WIDTH / 2;
            self.visoulView?.frame = (self.imageView?.bounds)!;
            
            self.visoulView?.alpha = abs(height)/130  * 0.25  + 0.75;
        }
    }



}
